package tech.aistar.corejava.day10;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author success
 * @version 1.0
 * @description:本类用来演示:日期工具类
 * @date 2019/7/26 0026
 */
@SuppressWarnings("all")
public class DateUtil {

    public static void main(String[] args) {
//        Calendar cal = Calendar.getInstance();
//        cal.set(2019,6,26);
//
//        //日历转成日期
//        Date d = cal.getTime();

        Date d = new Date(2019-1900,6,26);
        String str = DateUtil.format(d,"yyyy-MM-dd");
        System.out.println(str);

        System.out.println("=====");

        Date dt = DateUtil.parse("2012-01-09","yyyy-MM-dd");
        System.out.println(dt);
    }

    /**
     *  Date类型根据模板解析成字符串
     * @param d 传入的指定的Date对象
     * @param pattern 模板
     * @return 日期格式的字符串[根据模板出来的]
     */
    public static String format(Date d,String pattern){
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String str = sdf.format(d);
        return str;
    }

    /**
     * 将字符串解析成了Date类型
     * @param sdate
     * @param pattern
     * @return
     */
    public static Date parse(String sdate,String pattern){
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        Date d = null;
        try {
             d = format.parse(sdate);//alter+enter
        } catch (ParseException e) {
            //e.printStackTrace();
            System.out.println("解析失败异常!可能是字符串和模板不匹配!");
        }
        return d;
    }

    /**
     * 判断年是否为闰年
     */
    public static boolean isLeapYear(int year){
//        if((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)){
//            return true;
//        }else{
//            return false;
//        }

//        if((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
//            return true;
//        return false;

        return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
    }

    //语法方面
    public static int getMaxDays(int year,int month){
        //定义天数
        int maxDays = -1;

        //根据分数的区间来实现显示的内容
        switch (month){
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                maxDays = 31;
                break;
            case 2:
                boolean flag = isLeapYear(year);
                //maxDays = (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0) ? 29: 28;
                maxDays = flag ? 29 : 28;
                break;
            default:
                maxDays = 30;
                break;
        }
        return maxDays;
    }

    /**
     * 求出某年某月某日是周几
     * @param year
     * @param month
     * @param date
     * @return
     */
    public static int getDayOfWeeks(int year,int month,int date){
        //1. 通过year,month,date来构建一个Calendar对象
        Calendar cal = Calendar.getInstance();

        //2.void set(int year,int month,int date);
        //void set(int field,int value);
        cal.set(year,month-1,date);

        //3. 从cal日历信息中获取周几的字段
        //保证周日是0,周1到周6 - 数字1-6
        return cal.get(Calendar.DAY_OF_WEEK)-1;
    }
}
